package state.component;

import state.DrawListener;
import state.GameConstants;
import state.Graphics;
import state.KeySpriteListener;
import state.Manager;
import state.Sprite;
import state.TouchScreenListener;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;

public class Component extends Sprite implements DrawListener, KeySpriteListener, TouchScreenListener {
  protected boolean _fillBackGround = true;
  protected int _backgroundColor = GameColor.WHITE_CODE;

  protected boolean _enable = true;
  protected int _forceGroundColor = GameColor.BLACK_CODE;

  protected boolean _hasBorder = true;
  protected int borderColor = GameColor.RED2_CODE;

  protected boolean _focused = false;
  protected boolean _focusable = true;
  protected int _focusBgColor = GameColor.GREEN_CODE;
  protected int _focusFgColor = GameColor.RED2_CODE;

  private ImageText _font;

  /**
   * Constructor
   * 
   * @param manager1 - Manager dùng để quản lý việc hiển thị
   * @param x - Tọa độ x của Component
   * @param y - Tọa độ y của Component
   * @param width - Chiều rộng của Component
   * @param height - Chiều dài của Component
   */
  public Component(Manager manager1, int x, int y, int width, int height) {
    super(null, manager1, x, y, GameConstants.TOP_LEFT_ANCHOR);
    setSize(width, height);
    initComponent();
  }

  public void initComponent() {
    setDrawListener(this);
    setTouchScreenListener(this);
    setKeyListener(this);
  }

  public void draw(Graphics g) {
  }

  public void paint(Sprite source, Graphics g) {
    draw(g);
  }

  public void setManager(Manager manager1) {
    if (manager1 != null) {
      manager1.append(this);
    }
  }

  public Component setStyle(Style style) {
    _forceGroundColor = style.forceGroundColor;
    _backgroundColor = style.backgroundColor;
    _enable = style.enable;
    _hasBorder = style.hasBorder;
    borderColor = style.borderColor;
    _focused = style.focused;
    _focusable = style.focusable;
    _fillBackGround = style.fillBackGround;
    _focusBgColor = style.focusBgColor;
    _focusFgColor = style.focusFgColor;
    borderColor = style.borderColor;
    setFont(style.font);
    if (style.backgroundImage == null) {
      setImage1(null, false);
    } else {
      setImage1(style.backgroundImage, false);
    }
    return this;
  }

  public ImageText getFont() {
    if (_font == null) {
      _font = FontManager.getFont(FontManager.FONT_SIZE_8);
    }
    return _font;
  }

  public Component setFont(ImageText font1) {
    if (font1 == null) {
      return this;
    }

    this._font = font1;
    return this;
  }

  /** Focus back ground color */
  public int getFocusBgColor() {
    return _focusBgColor;
  }

  /** Focus back ground color */
  public Component setFocusBgColor(int focusBgColor1) {
    _fillBackGround = true;
    setFocusable(true);
    this._focusBgColor = focusBgColor1;
    return this;
  }

  /** Focus force ground color */
  public int getFocusFgColor() {
    return _focusFgColor;
  }

  /** Focus force ground color */
  public Component setFocusFgColor(int focusFgColor1) {
    this._focusFgColor = focusFgColor1;
    setFocusable(true);
    return this;
  }

  public int getBackgroundColor() {
    return _backgroundColor;
  }

  public Component setBackgroundColor(int backgroundColor1) {
    _fillBackGround = true;
    this._backgroundColor = backgroundColor1;
    return this;
  }

  public boolean isFillBackGround() {
    return _fillBackGround;
  }

  public Component setFillBackGround(boolean isFillBackGround) {
    this._fillBackGround = isFillBackGround;
    return this;
  }

  public int getForceGroundColor() {
    return _forceGroundColor;
  }

  public Component setForceGroundColor(int forceGroundColor1) {
    this._forceGroundColor = forceGroundColor1;
    return this;
  }

  public boolean isEnable() {
    return _enable;
  }

  public Component setEnable(boolean isEnable) {
    this._enable = isEnable;
    return this;
  }

  public boolean isHasBorder() {
    return _hasBorder;
  }

  public Component setHasBorder(boolean hasBorder1) {
    this._hasBorder = hasBorder1;
    return this;
  }

  public int getBorderColor() {
    return borderColor;
  }

  public Component setBorderColor(int borderColor1) {
    _hasBorder = true;
    this.borderColor = borderColor1;
    return this;
  }

  public boolean isFocused() {
    return _focused;
  }

  public Component setFocused(boolean isFocused) {
    if (_focusable) {
      this._focused = isFocused;
    }
    return this;
  }

  public boolean isFocusable() {
    return _focusable;
  }

  public Component setFocusable(boolean focusable1) {
    this._focusable = focusable1;
    if (!focusable1) {
      _focused = false;
    }
    return this;
  }

  public void keyPressed(Sprite source, int keyCode) {
  }

  public void keyReleased(Sprite source, int keyCode) {
  }

  public void keyRepeated(Sprite source, int keyCode) {
  }

  public void pointerDragged(Sprite source, int x1, int y1) {
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
  }

  public Sprite detroy() {
    super.detroy();
    _font = null;
    return this;
  }
}
